package org.hepeng.app;

import org.apache.commons.pool2.impl.AbandonedConfig;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
import org.hepeng.fabric.gateway.pool.FabricGatewayObject;
import org.hepeng.fabric.gateway.pool.GatewayKeyProvider;
import org.hepeng.fabric.gateway.pool.spring.FabricGatewayPoolConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;

import java.time.Duration;

/**
 * @author he peng
 * @date 2022/3/25
 */

@Configuration
@Import({FabricGatewayPoolConfiguration.class})
@AutoConfigureBefore({FabricGatewayPoolConfiguration.class})
public class GatewayPoolConfiguration {


    @Bean
    public GenericKeyedObjectPoolConfig<FabricGatewayObject> poolConfig() {

        GenericKeyedObjectPoolConfig<FabricGatewayObject> poolConfig = new GenericKeyedObjectPoolConfig<>();
        poolConfig.setMaxTotal(-1);
        poolConfig.setJmxEnabled(false);
        poolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(5));
        poolConfig.setSoftMinEvictableIdleTime(Duration.ofSeconds(1000000));
        poolConfig.setMinEvictableIdleTime(Duration.ofSeconds(1000000));
        return poolConfig;
    }


    @Bean
    public AbandonedConfig abandonedConfig() {
        AbandonedConfig abandonedConfig = new AbandonedConfig();
        abandonedConfig.setLogAbandoned(true);
        return abandonedConfig;
    }

    @Bean
    public GatewayKeyProvider gatewayKeyProvider(JdbcTemplate jdbcTemplate) {

        return new JdbcGatewayKeyProvider(jdbcTemplate);
    }
}
